﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using FeedAgregator;
using DDNY.WebPortals.Common;

public partial class BrowseExternalCategory : System.Web.UI.Page
{
    public int PageNumber { get; set; }

    public int SelectedListViewIndex
    {
        get
        {
            if (Session["SelectedListViewIndex"] != null)
            {
                return (int)Session["SelectedListViewIndex"];
            }
            else
            {
                return 0;
            }
        }

        set
        {
            Session["SelectedListViewIndex"] = value;
        }
    }

    public int CategoryId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {


        foreach (string key in Request.QueryString)
        {
            switch (key.ToLower())
            {
                case "id":
                    CategoryId = int.Parse(Request.QueryString[key]);
                    break;
                case "page":
                    PageNumber = int.Parse(Request.QueryString[key]);
                    break;
                default:
                    break;
            }
        }
        if (!IsPostBack)
        {
            SelectedListViewIndex = 0;
            LoadData(CategoryId, PageNumber);
        }

        if (!IsPostBack)
        {
            GenerateTitle(CategoryId, PageNumber);
        }
    }

    private void LoadData(int CategoryId, int PageNumber)
    {
        int pageSize = 45;
        FeedItemsListView.DataSource = DataAccessHelper.GetFeedItemsByExternalCategoryIdAndPageNumber(CategoryId, PageNumber, pageSize);
        FeedItemsListView.DataBind();
        //if (FeedItemsListView.Items.Count == pageSize)
        //{
        //    DataPager FeedItemsDataPager = FeedItemsListView.FindControl("FeedItemsDataPager") as DataPager;
        //    FeedItemsDataPager.
        //    FeedItemsDataPager.PageSize = pageSize;
        //    (FeedItemsDataPager.Fields[2] as NextPreviousPagerField).ShowNextPageButton = true;
        //}
    }



    private void GenerateTitle(int categoryId, int pageNumber)
    {
        string categoryName;



        categoryName = DataAccessHelper.GetExternalCategoryTitleById(categoryId);
        string title = string.Empty;
        if (pageNumber > 0)
        {
            title = categoryName + " - page " + pageNumber.ToString();
        }
        else
        {
            title = categoryName;
        }
        this.Title = "" + "" + title;
    }

    protected void FeedItemsListView_DataBound(object sender, EventArgs e)
    {
        if (FeedItemsListView.Items.Count > 0)
        {
            int feedItemID = int.Parse(FeedItemsListView.DataKeys[SelectedListViewIndex].Value.ToString());
            FeedItem feedItem = DataAccessHelper.GetFeedItemByID(feedItemID);
            HtmlContainerControl ItemContentDiv = FeedItemsListView.Items[SelectedListViewIndex].FindControl("ItemContentDiv") as HtmlContainerControl;
            if (feedItem.ItemContent != null)
            {
                feedItem.ItemContent = CommonUtils.SanitizeContent(feedItem.ItemContent, true);
                if (feedItem.ItemContent.Length > 2000)
                {
                    ItemContentDiv.InnerHtml = feedItem.ItemContent.Substring(0, 800);
                }
                else
                {
                    ItemContentDiv.InnerHtml = feedItem.ItemContent;
                }
            }

            ItemContentDiv.Visible = true;
        }
    }

    protected void FeedItemsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = e.Item as ListViewDataItem;
            object feedItem = dataItem.DataItem;
            DateTime pubDate;
            Type feedItemDynamicType = feedItem.GetType();
            PropertyInfo itemUrlPropertyInfo = feedItemDynamicType.GetProperty("PubDate");
            pubDate = (DateTime)itemUrlPropertyInfo.GetValue(feedItem, null);
            Label PubDateLabel = e.Item.FindControl("PubDateLabel") as Label;
            string formatDateString = ("dd.MM - HH:mm");
            if (pubDate.Date == DateTime.Now.Date)
            {
                formatDateString = ("HH:mm");
            }
            PubDateLabel.Text = pubDate.ToString(formatDateString);

        }

    }

    protected void FeedItemsListView_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
    {
        SelectedListViewIndex = e.NewSelectedIndex;
    }

}
